﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace DuocThaiBinh.Common
{
    public abstract class ViewAllWithDeleteViewPage<T> : WebViewPage
    {
        public ViewWithDeleteGenerator ViewWithDeleteGenerator { get; set; }
        public override void InitHelpers()
        {
            base.InitHelpers();
            ViewWithDeleteGenerator = new ViewWithDeleteGenerator();
        }

    }

    public class ViewWithDeleteGenerator
    {
        public MvcHtmlString GenView(string objectName, string linkCreate, string linkEdit, string linkDelete, string linkGetAllItems, string[] headers, string[] contents, string defaultSortIndex)
        {
            string path = HttpRuntime.AppDomainAppPath;
            path = Path.Combine(path, @"Views\Shared\ViewTemplate.txt");
            string content = File.ReadAllText(path);
            content = content.Replace("|#ObjectName#|", objectName);
            content = content.Replace("|#LinkCreate#|", linkCreate);
            string temp = string.Empty;
            foreach (string header in headers)
            {
                temp += string.Format("<th>{0}</th>", header) + Environment.NewLine;
            }
            content = content.Replace("|#LoopRowHeader#|", temp);
            temp = string.Empty;
            foreach (string c in contents)
            {
                temp += string.Format("{{ \"data\": \"{0}\" }},", c);
            }
            temp = temp.TrimEnd(',');
            content = content.Replace("|#LoopRowContent#|", temp);
            content = content.Replace("|#LinkGetAllItems#|", linkGetAllItems);
            content = content.Replace("|#LinkEdit#|", linkEdit);
            content = content.Replace("|#LinkDelete#|", linkDelete);
            content = content.Replace("|#DefaultSortIndex#|", defaultSortIndex);
            return new MvcHtmlString(content);
        }
    }
}